#include #include #include // --------------------------- // ----|1 PB0 PA0 40|---- JOYSTICK1 // ----|2 PB1 PA1 39|---- JOYSTICK2 // E ----|3 PB2 PA2 38|---- // RS ----|4 PB3 PA3 37|---- // R/W ----|5 PB4 PA4 36|---- DIOD1 // RTS ----|6 PB5 PA5 35|---- DIOD2 // CS2 ----|7 PB6 PA6 34|---- DIOD3 // CS1 ----|8 PB7 PA7 33|---- BUTTON1 // ----|9 32|---- // 5V ----|10 MEGA 32 31|---- GND // GND ----|11 AVCC 30|---- 5V // ----|12 PC7 29|---- BUTTON2 // ----|13 PC6 28|---- BUTTON3 // DB0 ----|14 PD0 PC5 27|---- RESERVED FOR JTAG // DB1 ----|15 PD1 PC4 26|---- RESERVED FOR JTAG // DB2 ----|16 PD2 PC3 25|---- RESERVED FOR JTAG // DB3 ----|17 PD3 PC2 24|---- RESERVED FOR JTAG // DB4 ----|18 PD4 PC1 23|---- BUTTON4 // DB5 ----|19 PD5 PC0 22|---- BUTTON5 // DB6 ----|20 PD6 PD7 21|---- DB7 // --------------------------- #define BUTTON1 0x80 //PA7 #define BUTTON2 0x80 //PC7 #define BUTTON3 0x40 //PC6 #define BUTTON4 0x02 //PC1 #define BUTTON5 0x01 //PC0 #define DIOD1 0x90 //PA4 #define DIOD2 0xa0 //PA5 #define DIOD3 0xc0 //PA6 /* mode options: 0 - Draw mode 1 - Erase mode 2 - Nothing mode */ short int mode; short int valueADCL; //Joystick value short int oldValueXY; //Old data short int yKord; //Coordinats for y short int xKord; //Coordinats for x short int yData; //Data for the given coordinats unsigned short int allowDraw; //Allowed to draw = 1, not allowed = 0 unsigned short int time; // Counter for the timer unsigned char matrix1[8][128]; //Represents the display //All subrutines---------------------------------------------- //All subrutiens are explained further down in the code static void button_active(); static void joystick_active(); static void clearScreen(); static void displayPowerOn(); static void displayPowerOff(); static void writeModeCS1_data(); static void writeModeCS2_data(); static void writeModeCS1_cmd(); static void writeModeCS2_cmd(); static void initPORTS(); static void setYXData(short int x,short int y,short int yD); static void setMartix(); static void draw(short int yD,short int blinkMode); static void setup_timer(); //End All subrutines------------------------------------------- /* * Timer interrupt */ ISR(TIMER0_OVF_vect) { time++; TCCR0 = 0x0; TIMSK = 0x0; setup_timer(); //Is it time to allow drawing again? if(time > 1){ //We are now allowed to draw again allowDraw = 1; time = 0; //Are we in nothing mode? if(mode==2){ //Draw the old value, blink mode on draw((matrix1[yKord][xKord]|(~oldValueXY)),1); }else{ //Draw the new value, blink mode off draw((matrix1[yKord][xKord]|(~yData)),1); } } else{ //Are we in nothing mode? if(mode==2){ //Draw the old value, blink mode on draw((matrix1[yKord][xKord]|(oldValueXY)),1); } else{ //Draw the new value, blink mode off draw((matrix1[yKord][xKord]&yData),1); } } } /* * Interrupt for the A/D converter */ ISR(ADC_vect) { //Read Joystick values valueADCL = ADCL; ADCSRA = 0xc8; } void main() { //Initiate the ports initPORTS(); //Reset on (L - activate) PORTB = 0x20; //Reset off in the rest of the program PORTB = 0xc4; PORTB = 0xe4; displayPowerOn(); //Set startPosition xKord = 0x30; yKord = 0x03; yData = 0xfe; //Set Mode do draw mode mode = 0; //Light the draw mode diod PORTA = DIOD1; allowDraw = 1; //Set interuptflag, and choose PA0 as first port to read ADCSRA = 0xc8; ADMUX = 0xc0; //Allow interupt SREG|= 0x80; sei(); clearScreen(); //Start timer setup_timer(); //Polling starts, see if buttons or if Joystick are active while(1) { //Are any buttons pushed? (PA7,PC7,PC6,PC1,PC0) if( ((~PINA) & BUTTON1)== BUTTON1 || ((~PINC) & BUTTON2)== BUTTON2 || ((~PINC) & BUTTON3)== BUTTON3 || ((~PINC) & BUTTON4)== BUTTON4 || ((~PINC) & BUTTON5)== BUTTON5 ) { button_active(); } //See what is happening to the Joystick joystick_active(); } } /* * Set the timer */ static void setup_timer() { //TimeCounterControlRegster TCCR0 = 0x4; // Use the fast timer TIMSK = 0x1; // Trig on overflow(bitmask for interrupt) TCNT0=0x0; } /* * See if the Joystick is active */ static void joystick_active(){ //Is the Joystick move up or down? (Any signals comming in om PA0) if((ADMUX & 0xff) == 0xc0){ //Is the joystick moved down? if( valueADCL <= 3 ){ //Are we allowed to draw if(allowDraw==1){ //Draw current position and move the marker one step down setYXData(0,0,1); //Now we are not allowed to draw again until timer sais so //(reduces the speed when reading the Joystick) allowDraw = 0; setup_timer(); } } //Is the Joystick in the middle mode? else if( (valueADCL >= 4) && (valueADCL <= 103) ){//Mittenläget 0x67 //Not used (method keept for future implementation) } //Is the joystick moved up? else if( valueADCL >= 104 ){ //UPP //Are we allowed to draw if(allowDraw==1){ //Draw current position and move the marker one step up setYXData(0,0,-1); //Now we are not allowed to draw again until timer sais so //(reduces the speed when reading the Joystick) allowDraw = 0; setup_timer(); } } //Set readmode to PA1 now and wait for timer to activate read ADMUX = 0xc1; //Is the Joystick move left or right? (Any signals comming in om PA1) }else if( (ADMUX & 0xff) == 0xc1){ //Is the joystick moved left? if( valueADCL <= 5 ){ //Are we allowed to draw if(allowDraw==1){ //Draw current position and move the marker one step left setYXData(-1,0,0); //Now we are not allowed to draw again until timer sais so //(reduces the speed when reading the Joystick) allowDraw = 0; setup_timer(); } } //Is the Joystick in the middle mode? else if( (valueADCL >= 6) && (valueADCL <= 103) ){//Mittenläget 0x67 //Not used (method keept for future implementation) } //Is the joystick moved right? else if( valueADCL >= 104 ){ //Are we allowed to draw if(allowDraw==1){ //Draw current position and move the marker one step right setYXData(1,0,0); //Now we are not allowed to draw again until timer sais so //(reduces the speed when reading the Joystick) allowDraw = 0; setup_timer(); } } //Set readmode to PA0 now and wait for timer to activate read ADMUX = 0xc0; } } /* * Set the coordinats for the marker, draw and save to matrix * x - Left to Right (-1 to 1) * y - Shift 8 bits up or down (not in use, remanes for future implementation * yD - Down and Up (-1 and 1) */ static void setYXData(short int x,short int y,short int yD){ //Draw the last corinate, blinkmode off draw(matrix1[yKord][xKord],0); //Will the coordinats be outside the display window (above) if(yData== 0xfe && yD==-1){ yKord += -1; yData= 0x7f; } //Will the coordinats be outside the display window (underneath) else if(yData== 0x7f && yD==1){ yKord += 1; yData= 0xfe; } //Are the coordinats in the display window else{ //If the joystick is moved down if(yD==1){ //Shift left yData = yData << 1; yData += 1-256; //If the joystick is moved up }else if(yD==-1){ //Shift right yData = yData>>1; yData = yData+0x80; } } //Change coordinats xKord += x; yKord += y; //Will the coordinats be outside the display window (right) if(xKord>127){ xKord=127; } //Will the coordinats be outside the display window (left) else if(xKord<0){ xKord=0; } //Will the coordinats be outside the display window (above) if(yKord < 0){ yKord = 0; yData= 0xfe; } //Will the coordinats be outside the display window (underneath) else if(yKord > 7){ yKord = 7; yData= 0x7f; } //Save our matrix setMartix(); } /* * Save the new values to our matrix */ static void setMartix(){ //Draw mode selected if(mode==0){ matrix1[yKord][xKord] = (matrix1[yKord][xKord]&yData); } //Erase mode selected else if (mode == 1){ matrix1[yKord][xKord] = (matrix1[yKord][xKord]|(~yData)); } //Nothing mode selected else if(mode == 2){ //Save old matrix value oldValueXY = matrix1[yKord][xKord]; } } /* * See what button that was pressed */ //En knapp är nedtryckt static void button_active() { //Is draw button pressed if((((~PINA) & BUTTON1) == BUTTON1)){ PORTA = DIOD1; mode = 0; } //Is erase button pressed else if((((~PINC) & BUTTON2) == BUTTON2)){ PORTA = DIOD2; mode = 1; } //Is nothing button pressed else if((((~PINC) & BUTTON3) == BUTTON3)){ PORTA = DIOD3; mode = 2; oldValueXY = matrix1[yKord][xKord]; } //Is clear button pressed else if((((~PINC) & BUTTON4) == BUTTON4)){ clearScreen(); } //Is extra button pressed else if((((~PINC) & BUTTON5) == BUTTON5)){ //Not used (method keept for future implementation) } } /* * Draw 8 pixels to the display screen */ static void draw(short int yD, short int blinkMode){ //Is draw nothing mode on if(mode==2 && blinkMode==0){ yD = oldValueXY; } //Are we on the left side of the display window? if(xKord<64){ //Set coordinats PORTD = 0x40+xKord; writeModeCS1_cmd(); //Set coordinats PORTD = 0xb8 + yKord; writeModeCS1_cmd(); //Set bits, and draw PORTD = yD; writeModeCS1_data(); } //Are we on the right side of the display window? else if(xKord>63){ //Set coordinats PORTD = 0x40+xKord-64; writeModeCS2_cmd(); //Set coordinats PORTD = 0xb8 + yKord; writeModeCS2_cmd(); //Set bits, and draw PORTD = yD; writeModeCS2_data(); } } /* * Write command to the left side of the display */ static void writeModeCS1_cmd(){ // PB7 PB6 PB5 PB4 PB3 PB2 PB1 PB0 // CS1 CS2 RTS R/W RS E ? ? // 1 1 1 0 0 1 0 0 => 0xe4 PORTB = 0xe4; // PB7 PB6 PB5 PB4 PB3 PB2 PB1 PB0 // CS1 CS2 RTS R/W RS E ? ? // 0 1 1 0 0 0 0 0 => 0x60 PORTB = 0x60; // PB7 PB6 PB5 PB4 PB3 PB2 PB1 PB0 // CS1 CS2 RTS R/W RS E ? ? // 0 1 1 0 0 1 0 0 => 0x64 PORTB = 0x64; // PB7 PB6 PB5 PB4 PB3 PB2 PB1 PB0 // CS1 CS2 RTS R/W RS E ? ? // 0 1 1 0 0 0 0 0 => 0x60 PORTB = 0x60; // PB7 PB6 PB5 PB4 PB3 PB2 PB1 PB0 // CS1 CS2 RTS R/W RS E ? ? // 1 1 1 0 0 1 0 0 => 0xe4 PORTB = 0xe4; } /* * Write command to the right side of the display */ static void writeModeCS2_cmd(){ // PB7 PB6 PB5 PB4 PB3 PB2 PB1 PB0 // CS1 CS2 RTS R/W RS E ? ? // 1 1 1 0 0 1 0 0 => 0xe4 PORTB = 0xe4; // PB7 PB6 PB5 PB4 PB3 PB2 PB1 PB0 // CS1 CS2 RTS R/W RS E ? ? // 1 0 1 0 0 0 0 0 => 0xa0 PORTB = 0xa0; // PB7 PB6 PB5 PB4 PB3 PB2 PB1 PB0 // CS1 CS2 RTS R/W RS E ? ? // 1 0 1 0 0 1 0 0 => 0xa4 PORTB = 0xa4; // PB7 PB6 PB5 PB4 PB3 PB2 PB1 PB0 // CS1 CS2 RTS R/W RS E ? ? // 1 0 1 0 0 0 0 0 => 0xa0 PORTB = 0xa0; // PB7 PB6 PB5 PB4 PB3 PB2 PB1 PB0 // CS1 CS2 RTS R/W RS E ? ? // 1 1 1 0 0 1 0 0 => 0xe4 PORTB = 0xe4; } /* * Write data to the left side of the display */ static void writeModeCS1_data(){ // PB7 PB6 PB5 PB4 PB3 PB2 PB1 PB0 // CS1 CS2 RTS R/W RS E ? ? // 1 1 1 0 0 1 0 0 => 0xe4 PORTB = 0xe4; // PB7 PB6 PB5 PB4 PB3 PB2 PB1 PB0 // CS1 CS2 RTS R/W RS E ? ? // 0 1 1 0 1 0 0 0 => 0x68 PORTB = 0x68; // PB7 PB6 PB5 PB4 PB3 PB2 PB1 PB0 // CS1 CS2 RTS R/W RS E ? ? // 0 1 1 0 1 1 0 0 => 0x6c PORTB = 0x6c; // PB7 PB6 PB5 PB4 PB3 PB2 PB1 PB0 // CS1 CS2 RTS R/W RS E ? ? // 0 1 1 0 1 0 0 0 => 0x68 PORTB = 0x68; // PB7 PB6 PB5 PB4 PB3 PB2 PB1 PB0 // CS1 CS2 RTS R/W RS E ? ? // 1 1 1 0 0 1 0 0 => 0xe4 PORTB = 0xe4; } /* * Write data to the right side of the display */ static void writeModeCS2_data(){ // PB7 PB6 PB5 PB4 PB3 PB2 PB1 PB0 // CS1 CS2 RTS R/W RS E ? ? // 1 1 1 0 0 1 0 0 => 0xe4 PORTB = 0xe4; // PB7 PB6 PB5 PB4 PB3 PB2 PB1 PB0 // CS1 CS2 RTS R/W RS E ? ? // 1 0 1 0 1 0 0 0 => 0xa8 PORTB = 0xa8; // PB7 PB6 PB5 PB4 PB3 PB2 PB1 PB0 // CS1 CS2 RTS R/W RS E ? ? // 1 0 1 0 1 1 0 0 => 0xac PORTB = 0xac; // PB7 PB6 PB5 PB4 PB3 PB2 PB1 PB0 // CS1 CS2 RTS R/W RS E ? ? // 1 0 1 0 1 0 0 0 => 0xa8 PORTB = 0xa8; // PB7 PB6 PB5 PB4 PB3 PB2 PB1 PB0 // CS1 CS2 RTS R/W RS E ? ? // 1 1 1 0 0 1 0 0 => 0xe4 PORTB = 0xe4; } /* * Activate the power to the display */ static void displayPowerOn(){ // PD7 PD6 PD5 PD4 PD3 PD2 PD1 PD0 // BD7 BD6 BD5 BD4 BD3 BD2 BD1 BD0 // 0 0 1 1 1 1 1 1 => 0x3f PORTD = 0x3f; //Turn on the left side writeModeCS1_cmd(); //Turn on the right side writeModeCS2_cmd(); } /* * Deactivate the power to the display */ static void displayPowerOff(){ // PD7 PD6 PD5 PD4 PD3 PD2 PD1 PD0 // BD7 BD6 BD5 BD4 BD3 BD2 BD1 BD0 // 0 0 1 1 1 1 1 0 => 0x3e PORTD = 0x3e; //Turn off the left side writeModeCS1_cmd(); //Turn off the right side writeModeCS2_cmd(); } /* * Clear the display screen and the matrix that represent the screen */ static void clearScreen(){ unsigned short int i; unsigned short int j; //Reset the matrix for(i = 0; i<8; i++){ for(j = 0; j<128; j++){ matrix1[i][j] = 0xff; } } i = 0; j = 0; //Set all values on the display to its initial value for(i = 0; i<64; i++){ for(j = 0; j<8; j++){ // Set Page Y address // PD7 PD6 PD5 PD4 PD3 PD2 PD1 PD0 // BD7 BD6 BD5 BD4 BD3 BD2 BD1 BD0 // 0 1 0 0 0 0 0 0 => 0x40 PORTD = 0x40+i; writeModeCS1_cmd(); writeModeCS2_cmd(); // Set Page X address // PD7 PD6 PD5 PD4 PD3 PD2 PD1 PD0 // BD7 BD6 BD5 BD4 BD3 BD2 BD1 BD0 // 1 0 1 1 1 0 0 0 => 0xb8 PORTD = 0xb8 + j; writeModeCS1_cmd(); writeModeCS2_cmd(); // PD7 PD6 PD5 PD4 PD3 PD2 PD1 PD0 // BD7 BD6 BD5 BD4 BD3 BD2 BD1 BD0 // 1 0 1 0 1 0 1 0 => 0xaa PORTD = 0xff; writeModeCS1_data(); writeModeCS2_data(); } } } /* * Initiate the ports */ static void initPORTS(){ //Activate PA: // PA7 PA6 PA5 PA4 PA3 PA2 PA1 PA0 // KN1 DIOD DIOD DIOD X X JOY JOY // 0 1 1 1 0 0 1 1 => 0x73 DDRA = 0x73; PORTA = 0x80; //Activate PB: // PB7 PB6 PB5 PB4 PB3 PB2 PB1 PB0 // CS1 CS2 RTS R/W RS E X X // 1 1 1 1 1 1 0 0 => 0xfc DDRB = 0xfc; //Activate PC: // PC7 PC6 PC5 PC4 PC3 PC2 PC1 PC0 // KN2 KN3 JTAG JTAG JTAG JTAG KN4 KN5 // 0 0 0 0 0 0 0 0 => 0x00 DDRC = 0x00; PORTC = 0xc3; //Activate PD: // PD7 PD6 PD5 PD4 PD3 PD2 PD1 PD0 // BD7 BD6 BD5 BD4 BD3 BD2 BD1 BD0 // 1 1 1 1 1 1 1 1 => 0xff DDRD = 0xff; }